package com.nowcoder.community.controller;/**
 * @description:
 * @Author HP
 * @create 2021/12/12 14:21
 */

import com.nowcoder.community.service.AlphaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @author HP
 * @create 2021-12-12 14:21
 * @desc controller示例1
 **/
@Controller
@RequestMapping("/alpha")
public class AlphaController {

    @Autowired
    private AlphaService alphaService;

    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello(){
        return "hello spring boot";
    }


    @RequestMapping("/data")
    @ResponseBody
    public String getData(){
        return alphaService.find();
    }

    @RequestMapping("/http")
    public void http(HttpServletRequest request, HttpServletResponse response){
        //获得请求数据
        System.out.println(request.getMethod());
        System.out.println(request.getServletPath());
        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()){
            String name= enumeration.nextElement();
            String value=request.getHeader(name);
            System.out.println(name+":"+value);
        }
        System.out.println(request.getParameter("code"));

        //返回响应数据
        PrintWriter printWriter=null;
        try {
            response.setContentType("text/html;charset=utf-8");
            printWriter= response.getWriter();
            printWriter.write("<h1>牛客网</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printWriter!=null){
                try {
                    printWriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //get请求

    // /student?current=1&limit=20
    @RequestMapping(path = "/students",method = RequestMethod.GET)
    @ResponseBody
    public String getStudent(
            @RequestParam(name = "current",required = false,defaultValue = "1") int current,
            @RequestParam(name = "limit",required = false,defaultValue = "20") int limit){
        System.out.println(current);
        System.out.println(limit);
        return "some Students";
    }

    // /student/123
    @RequestMapping(path = "/student/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getAStudent(@PathVariable(name = "id") int id){
        System.out.println(id);
        return "a sudent";
    }

    //post
    @RequestMapping(path = "/student",method = RequestMethod.POST)
    @ResponseBody
    public String saveStudent(String studentName,int age){
        System.out.println(studentName+":"+age);
        return "success!";
    }

    //响应html
    //方式一
    @RequestMapping(path = "/teacher",method = RequestMethod.GET)
    public ModelAndView getTeacher(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("name","张三");
        mav.addObject("age","30");
        mav.setViewName("/demo/view");
        return mav;
    }

    //方式二
    @RequestMapping(path = "/school",method = RequestMethod.GET)
    public String getSchool(Model model){
        model.addAttribute("name","北京大学");
        model.addAttribute("age","90");
        return "/demo/view";
    }

    //响应Jason数据，用于异步请求
    //java ---->jason  ----->js
    //返回单个对象
    @RequestMapping(path = "/emp",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getEmp(){
        Map<String,Object> map=new HashMap<>();
        map.put("name","zhangsan");
        map.put("age",13);
        map.put("salary",8000.00);
        return map;
    }

    //返回数组
    @RequestMapping(path = "/emps",method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> getEmps(){
        List<Map<String,Object>> list=new ArrayList<>();

        Map<String,Object> map=new HashMap<>();
        map.put("name","zhangsan");
        map.put("age",13);
        map.put("salary",8000.00);
        list.add(map);

        map=new HashMap<>();
        map.put("name","lisi");
        map.put("age",25);
        map.put("salary",80010.00);
        list.add(map);

        map=new HashMap<>();
        map.put("name","wangwu");
        map.put("age",41);
        map.put("salary",18000.00);
        list.add(map);

        return list;
    }


}
